-
Notifications
You must be signed in to change notification settings - Fork 0
Fix database bugs #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Added camelCase to snake_case conversion. - Improved sortBy parameter handling. - Enhanced query parameter parsing.
When creating new headlines or sources, the API was failing with a database error because it tried to insert nested `source`, `category`, and `headquarters` objects directly into the database. The database schema expects flattened foreign key columns (`source_id`, `category_id`, `headquarters_country_id`). This fix updates the `toJson` functions provided during the setup of the `headlineRepository` and `sourceRepository` in `AppDependencies`. These custom functions now correctly flatten the nested objects into their respective ID fields before the data is sent to the database client, resolving the "column does not exist" error.
Updated the `headlines` table schema to allow `source_id` and `category_id` to be nullable, which correctly reflects the optional `source` and `category` fields in the shared `Headline` model. Foreign key constraints have been added to these columns to maintain referential integrity when a value is present. The database seeding logic has also been updated to handle the potential for null values in these fields during fixture insertion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @fulleni, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request addresses several database-related issues by updating the database schema, adjusting data serialization logic, and improving API query parameter handling. These changes ensure data integrity and compatibility between the application and the database, while also providing more flexibility in data seeding.
Highlights
- Database Schema Update: Modified the
headlines
table schema to allowsource_id
andcategory_id
to be nullable, establishing foreign key relationships with thesources
andcategories
tables. - Data Serialization: Adjusted the serialization logic for
Headline
andSource
objects to handle nested objects correctly when interacting with the database. This involves extracting IDs from nestedsource
,category
, andheadquarters
objects and removing the original nested objects before saving to the database. - API Query Parameter Handling: Implemented a function to convert camelCase query parameters (e.g.,
createdAt
) to snake_case (e.g.,created_at
) for database queries, ensuring compatibility between client requests and database schema. - Data Seeding: Updated the database seeding service to accommodate nullable
source_id
andcategory_id
fields in theheadlines
table, removing the previous requirement for these fields to be non-null during seeding.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request fixes database bugs by updating the database schema to allow nullable foreign keys and converting API query parameters from camelCase to snake_case. There is some code duplication introduced in the repository toJson
converters and the DatabaseSeedingService
that should be addressed.
source_id TEXT REFERENCES sources(id), | ||
category_id TEXT REFERENCES categories(id), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(headline) { | ||
final json = headline.toJson(); | ||
// The database expects source_id and category_id, not nested objects. | ||
// We extract the IDs and remove the original objects to match the | ||
// schema. | ||
if (headline.source != null) { | ||
json['source_id'] = headline.source!.id; | ||
} | ||
if (headline.category != null) { | ||
json['category_id'] = headline.category!.id; | ||
} | ||
json.remove('source'); | ||
json.remove('category'); | ||
return json; | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change introduces a duplication of logic. The logic to prepare Headline
objects for the database now exists here in the repository configuration and also in lib/src/services/database_seeding_service.dart
(lines 265-266).
This duplication can lead to maintenance issues, where a change in the database schema would require updates in multiple places. To improve maintainability, consider centralizing this database-specific serialization logic, such as by adding a toDbJson()
method to the Headline
data model.
(source) { | ||
final json = source.toJson(); | ||
// The database expects headquarters_country_id, not a nested object. | ||
// We extract the ID and remove the original object to match the | ||
// schema. | ||
json['headquarters_country_id'] = source.headquarters?.id; | ||
json.remove('headquarters'); | ||
return json; | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change introduces a duplication of logic. The logic to prepare Source
objects for the database now exists here in the repository configuration and also in lib/src/services/database_seeding_service.dart
(lines 232-233).
To improve maintainability, consider centralizing this database-specific serialization logic, such as by adding a toDbJson()
method to the Source
data model.
@@ -109,10 +119,15 @@ Future<Response> _handleGet( | |||
final queryParams = context.request.uri.queryParameters; | |||
final startAfterId = queryParams['startAfterId']; | |||
final limitParam = queryParams['limit']; | |||
final sortBy = queryParams['sortBy']; | |||
final sortByParam = queryParams['sortBy']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Status
READY/IN DEVELOPMENT/HOLD
Description
Type of Change